<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Simplelogin
{
	var $CI;
	var $userTable;    
	function Simplelogin()
	{
		$this->CI =& get_instance();        $this->userTable = Doctrine::getTable('Users');
	}    
	function create($user = '', $password = '', $auto_login = true) {

		if($user == '' OR $password == '') {
			return false;
		}
        $query = $this->getUserByName($user);

		if ($query->count() > 0) {
			return false;		} else {		
			$password = md5($password);            $user = new Users();            $user->username = $user;            $user->password = $password;            if(!$user->save()) {				return false;			}
			if($auto_login) {		
				$this->CI->session->sess_destroy();				$this->CI->session->sess_create();				$this->CI->session->set_userdata(array('id' => $user->id, 'username' => $user->username));				$this->CI->session->set_userdata(array('logged_in' => true));						}					return true;		}	}
	function delete($user_id) {
		if(!is_numeric($user_id)) {			return false;					}        $user = getUser($user);		if(!$user->delete()) {				return false;									}	}
	function login($user = '', $password = '') {
		if($user == '' OR $password == '') {			return false;		}        $users = $this->getUserByName($user);        
        if(md5($password) != $users->password) {            return false;        }                if($users->is_active == 0) {            return false;        }		$users->last_login = date("Y-m-d H:i:s");		$users->save();				$data = array(						"name" => $users->name,						"is_super_admin" => $users->is_super_admin,						"last_login" => $users->last_login		);        $this->CI->session->sess_destroy();        $this->CI->session->sess_create();        $this->CI->session->set_userdata($data);        $this->CI->session->set_userdata(array('logged_in' => true));		return true;	}	function logout() {		$this->CI->session->sess_destroy();	}	function getUserByName($id, $array = false)		{			$users = $this->userTable->findOneByUsername($id);			if($users !== false) {				if ($array) {					return $users->toArray(true);				}else{					return $users;				}			}else{				show_404('page');			}		}	function getUser($id, $array = false)		{			$users = $this->userTable->find($id);			if($users !== false) {				if ($array) {					return $users->toArray(true);				}else{					return $users;				}			}else{				show_404('page');			}		}}